<template>
  <div style="padding-top: 50px;">
    <div>
      <el-button
      v-for="(item, index) in chekuan"
      :key='item.key'
      :type='item.state'
      :style="{color: item.color}"
      @click="toggle(index)"
      >{{item.name}}</el-button>
    </div>
    <div :id="chartsId" :style="{width: '900px', height: '500px'}" class="my-chart"></div>
  </div>
</template>

<style lang="scss" scoped>
.el-button{
  margin-right: 15px;
  color: #333;
}
</style>


<script>
export default {
  props: ['chartsId', 'data', 'val', 'name'],
  data(){
      return {
          chekuan: []
      }
  },
  watch:{
      data(){
					let val = this.val;
					let name = this.name;
					
          //nav数据
          this.chekuan = [];
          for(let i = 0; i < val.length; i++){
              let obj = {};
              obj.name = name[i];
              obj.color = '#333';
              obj.state = 'text';
              obj.key = val[i];
              this.chekuan.push(obj);
          }
          this.chekuan[0].color = '#409EFF';
          let benpin = 'discounts_benpin_' + this.val[0];
          let benpinId = this.data[benpin];
          let myChart = this.$echarts.init(document.getElementById(this.chartsId));
          myChart.setOption({
							xAxis: {
									type : 'category',
									splitLine: {show:false},
									data: this.data.date
							},
              series: [
                  {
											data: function(){
											let arr = [];
											let arrdata = benpinId.minDiscount;
											for(let i = 0; i < arrdata.length; i++){
												let obj = {};
												obj.value = arrdata[i];
												let str = 'rgb(245, 245, 245,0)';
												let str1 = 'rgba(0,0,0,0)'
												if(arrdata[i] <0) {
													str = '#c23532';
													str1 = '#c23532';
												}
												obj.itemStyle = {
													normal: {
                            barBorderColor: str,
                            color: str
													},
													emphasis: {
															barBorderColor: str1,
															color: str1
                        	}
												}
												arr.push(obj);
											}
											return arr;
										}()
                  },
                  {
											data: benpinId.maxDiscount
                  },
                  {
                      data: benpinId.avgDiscount
                  }
              ]
          })
      }
  },
  mounted(){
      this.desc();
  },
  methods: {
      toggle(index){
				  for(let i = 0; i < this.chekuan.length; i++){
						this.chekuan[i].color = '#333';
						if(i == index){
							this.chekuan[i].color = '#409EFF'
						}
					}
					let productId;
          if(index == 0){
              productId = 'discounts_benpin_' + this.val[0];
          }else{
              productId = 'discounts_jingpin_' + this.val[index];
          }
					let product = this.data[productId];
          let myChart = this.$echarts.init(document.getElementById(this.chartsId));
          myChart.setOption({
							xAxis: {
										type : 'category',
										splitLine: {show:false},
										data: this.data.date
							},
              series: [
                  {
											// data: product.minDiscount
											data: function(){
											let arr = [];
											let arrdata = product.minDiscount;
											
											for(let i = 0; i < product.avgDiscount.length; i++){
												let obj = {};
												obj.value = arrdata[i];
												let str = 'rgb(245, 245, 245,0)';
												let str1 = 'rgba(0,0,0,0)'
												if(arrdata[i] <0) {
													str = '#c23532';
													str1 = '#c23532';
												}
												obj.itemStyle = {
													normal: {
                            barBorderColor: str,
                            color: str
													},
													emphasis: {
															barBorderColor: str1,
															color: str1
                        	}
												}
												arr.push(obj);
											}
											return arr;
										}()
                  },
                  {
                      data: product.maxDiscount
                  },
                  {
                      data: function(){
												
												return product.avgDiscount;
											}()
                  }
              ]
          })
      },
      desc(){
          let myChart = this.$echarts.init(document.getElementById(this.chartsId));
          myChart.setOption({
            title: {
                text: '价格优惠区间图',
            },
            tooltip : {
                trigger: 'axis',
                axisPointer : {            // 坐标轴指示器，坐标轴触发有效
                    type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                },
                formatter: function (params) {
									
                    var tar0 = params[0];
                    var tar1 = params[1];
										var tar2 = params[2];
										return tar0.name + '<br/>' + tar1.seriesName + ' : ' + (tar1.value+tar0.value) + '<br/>' +  tar0.seriesName + ' : ' + tar0.value + '<br/>' + tar2.seriesName + ' : ' + tar2.value;
                }
            },
            legend: {
                data:['支出','收入']
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: {
                type : 'category',
                splitLine: {show:false},
                data: this.data.date
            },
            yAxis: {
                type : 'value',
                name: '万元'
            },
            series: [
                {
                    name: '一般优惠',
                    type: 'bar',
                    stack: '总量',
                    label: {
                        normal: {
                            show: true,
                            position: 'top',
                            color: '#ccc'
                        }
                    },
                    itemStyle: {
                        normal: {
                            barBorderColor: 'rgb(245, 245, 245,0)',
                            color: 'rgb(245, 245, 245,0)'
                        },
                        emphasis: {
                            barBorderColor: 'rgba(0,0,0,0)',
                            color: 'rgba(0,0,0,0)'
                        }
                    },
                },
                {
                    name: '最高优惠',
                    type: 'bar',
										stack: '总量',
										itemStyle: {
                        normal: {
                            barBorderColor: '#c23532',
                            color: '#c23532'
                        },
                        emphasis: {
                            barBorderColor: '#c23532',
                            color: '#c23532'
                        }
                    },
                },
                {
                    name:'平均优惠',
                    type:'line',
                }
            ]
        })


      }
  }
}
</script>
